// pages/wristhistory/index.js
const app=getApp();
const config=app.globalData;
Page({
    /**
     * 页面的初始数据
     */
    data: {
        historyList: [],
        pageNum: 1,
        pageSize: 25,
        total: 0,
        currtTime: null,
        isLoading: false,
    },
    // 上拉触发刷新
    getUpData(){
        let that = this;
        if(that.data.isLoading){
            return ;
        }
        wx.showLoading({
            title: '正在刷新...',
        });
        let time = that.formatTime(new Date());
        that.setData({
            isLoading: true,
            currtTime: time,
            pageNum: 1,
            total: 0,
            historyList: [],
        });
        wx.request({
            url: config.url+'api/equipment/getHistory?equipmentNo='+config.userInfo.equipmentNo+'&phone='+config.userInfo.phone+'&equipmentType='+config.userInfo.equipmentType+'&pageNum='+that.data.pageNum+'&pageSize='+that.data.pageSize+'&startTime='+that.data.currtTime,
            success(res){
                let data = res.data.rows;
                let total = res.data.total;
                that.setData({
                    historyList: data,
                    total: total,
                });
            },
            complete: ()=>{
                wx.hideLoading();
                that.setData({
                    isLoading: false
                });
            }
        });
    },
    // 下拉触发加载
    getDownData(){
        if(this.data.isLoading){
            return ;
        }
        let that = this;
        if(this.data.pageSize * (this.data.pageNum) < this.data.total){
            wx.showLoading({
                title: '数据加载中...',
            });
            this.setData({
                isLoading: true,
            });
            let page = that.data.pageNum + 1;
            wx.request({
                url: config.url+'api/equipment/getHistory?equipmentNo='+config.userInfo.equipmentNo+'&phone='+config.userInfo.phone+'&equipmentType='+config.userInfo.equipmentType+'&pageNum='+page+'&pageSize='+that.data.pageSize+'&startTime='+that.data.currtTime,
                success(res){
                    let data = res.data.rows;
                    that.setData({
                        historyList: [...that.data.historyList, ...data],
                        pageNum: page,
                        total: res.data.total
                    });
                },
                complete:() => {
                    wx.hideLoading();
                    this.setData({
                        isLoading:false
                    });
                }
            })
        }else{
            wx.showToast({
                title: "没有数据了",
                icon: 'loading',
                duration: 1000
            })
        }
    },
    upper(e){
        if(e.detail.direction == "top"){
            this.getUpData()
        }
    },
    lower(e){
        if(e.detail.direction == "bottom"){
            this.getDownData()
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            currtTime:this.formatTime(new Date())
        });
        this.getUpData();
    },
    formatTime(date) {
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
    
        var hour = date.getHours();
        var minute = date.getMinutes();
        var second = date.getSeconds();
    
        return [year, month, day].map(this.formatNumber).join('-') + ' ' + [hour, minute, second].map(this.formatNumber).join(':');
    },
    formatNumber(n) {
        n = n.toString();
        return n[1] ? n : '0' + n;
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})